from targetmonitoring.abstracts.abstract_submit_to_target_client import AbstractSubmitToTargetClient
import targetmonitoring.utils as utils

class ConsoleSubmitToTargetClient(AbstractSubmitToTargetClient):
    def __init__(self, target_csv_file, target_object):
        super(ConsoleSubmitToTargetClient, self).__init__(target_csv_file, target_object)
    
    def _on_display_requested(self, selection_list):
        print "<---------Target Submit----------->"
        for menu_item in selection_list:
            print "%s"%(menu_item)
            

        self._fire_item_selected(self._get_selection(len(selection_list)))
    
    def _on_target_submit_request(self, target_submit_class, diff=0):
        target_submit = target_submit_class()
        for field in target_submit.get_fields():
            given = raw_input("Enter " + field[1]+":")
            setattr(target_submit,field[0], given)

        self._fire_on_submit_completed(target_submit, diff)

    @utils.clear_console
    def _on_exit(self):
        pass      
    
    def _get_selection(self, ra):
        chosen = raw_input("Select "+ str(range(ra)) + ":")
        try:
            chosen = int(chosen)
            if 0<=chosen<ra :
                return chosen
        except ValueError:
            pass
                    
        return None
        

if __name__ == '__main__':
    client = ConsoleSubmitToTargetClient('/tmp/1373263589708.csv')
    client.start()
